/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package demo.axitdn.event;

//import net.axitdn.control.*;
/**
 * 
 * @author Nguyen Ngoc Tu - FPT-Aptech; Hanoi-Vietnam;
 * @email: axitdn@gmail.com
 */
public class BaseMultiEventListener implements EventListener {

	protected final EventListener a, b;

	protected BaseMultiEventListener(EventListener a, EventListener b) {
		this.a = a;
		this.b = b;
	}

	protected static EventListener addInternal(EventListener a, EventListener b) {
		if (a == null)
			return b;
		if (b == null)
			return a;
		return new BaseMultiEventListener(a, b);
	}

	protected static EventListener removeInternal(EventListener l,
			EventListener oldl) {
		if (l == oldl || l == null) {
			return null;
		} else if (l instanceof BaseMultiEventListener) {
			return ((BaseMultiEventListener) l).remove(oldl);
		} else {
			return l; // it's not here
		}
	}

	protected EventListener remove(EventListener oldl) {
		if (oldl == a)
			return b;
		if (oldl == b)
			return a;
		EventListener a2 = removeInternal(a, oldl);
		EventListener b2 = removeInternal(b, oldl);
		if (a2 == a && b2 == b) {
			return this; // it's not here
		}
		return addInternal(a2, b2);
	}

}
